/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core.command.invoker;

import static junit.framework.Assert.assertTrue;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;

import java.util.LinkedList;
import java.util.List;

import org.junit.Test;
import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.mock.AnnotatedCommand;
import org.openinsula.vulcano.core.command.mock.ConfigurableCommand;
import org.openinsula.vulcano.core.command.mock.InitializingBeanCommandInterface;
import org.openinsula.vulcano.core.command.mock.MockType;
import org.openinsula.vulcano.core.command.mock.PostConstructCommandInterface;
import org.openinsula.vulcano.core.command.mock.SubtypeOfTransactionalOnDeclarationDICommand;
import org.openinsula.vulcano.core.command.mock.SubtypeOfTransactionalOnExecuteDICommand;
import org.openinsula.vulcano.core.command.mock.TransactionalOnDeclarationDICommand;
import org.openinsula.vulcano.core.command.mock.TransactionalOnExecuteDICommand;
import org.openinsula.vulcano.core.command.processor.CommandProcessor;
import org.openinsula.vulcano.core.command.processor.DependencyInjectionCommandProcessor;
import org.openinsula.vulcano.core.command.processor.TransactionalCommandProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

@ContextConfiguration
public class LocalCommandInvokerTestCase extends AbstractJUnit4SpringContextTests implements BeanFactoryAware {

	private BeanFactory beanFactory;

	protected CommandInvoker createLocalCommandInvoker() {
		LocalCommandInvoker commandInvoker = new LocalCommandInvoker();
		List<CommandProcessor> commandProcessorList = new LinkedList<CommandProcessor>();

		DependencyInjectionCommandProcessor diCommandProcessor = new DependencyInjectionCommandProcessor();
		diCommandProcessor.setBeanFactory(beanFactory);
		commandProcessorList.add(diCommandProcessor);

		TransactionalCommandProcessor tCommandProcessor = new TransactionalCommandProcessor();
		PlatformTransactionManager txManager = createMock(PlatformTransactionManager.class);
		expect(txManager.getTransaction((TransactionDefinition) anyObject())).andReturn(createNiceMock(TransactionStatus.class));
		txManager.commit((TransactionStatus) anyObject());
		replay(txManager);

		tCommandProcessor.setTransactionManager(txManager);
		commandProcessorList.add(tCommandProcessor);

		commandInvoker.setCommandProcessorList(commandProcessorList);
		return commandInvoker;
	}

	@Test
	public void testDependencyInjectionOnAnnotatedCommand() {
		AnnotatedCommand command = new AnnotatedCommand();
		assertTrue(createLocalCommandInvoker().invoke(command));
	}

	@Test
	public void testDependencyInjectionOnConfigurableCommand() {
		ConfigurableCommand command = new ConfigurableCommand();
		assertTrue(createLocalCommandInvoker().invoke(command));
	}

	@Test
	public void testNoDependencyInjectionOnCommand() {
		Command<Boolean> command = new MockCommand();

		assertTrue(createLocalCommandInvoker().invoke(command));
	}
	
	private static final class MockCommand implements Command<Boolean> {
		private static final long serialVersionUID = 1L;

		private MockType mockType;

		@SuppressWarnings("unused")
		public void setMockType(MockType mockType) {
			this.mockType = mockType;
		}

		@Override
		public Boolean execute() {
			return mockType == null;
		}

	}

	@Test
	public void testTransactionalOnDeclarationCommand() {
		assertTrue(createLocalCommandInvoker().invoke(new TransactionalOnDeclarationDICommand()));
	}

	@Test
	public void testTransactionalOnExecuteCommand() {
		assertTrue(createLocalCommandInvoker().invoke(new TransactionalOnExecuteDICommand()));
	}

	@Test
	public void testSubtypeOfTransactionalOnDeclarationCommand() {
		assertTrue(createLocalCommandInvoker().invoke(new SubtypeOfTransactionalOnDeclarationDICommand()));
	}

	@Test
	public void testSubtypeOfTransactionalOnExecuteCommand() {
		assertTrue(createLocalCommandInvoker().invoke(new SubtypeOfTransactionalOnExecuteDICommand()));
	}

	@Test
	public void testInitializingBeanCommand() throws Exception {
		InitializingBeanCommandInterface mockCommand = createMock(InitializingBeanCommandInterface.class);
		mockCommand.afterPropertiesSet();
		expect(mockCommand.execute()).andReturn(true);
		replay(mockCommand);

		assertTrue(createLocalCommandInvoker().invoke(mockCommand));
	}

	@Test
	public void testPostConstructCommand() {
		PostConstructCommandInterface mockCommand = createMock(PostConstructCommandInterface.class);
		mockCommand.init();
		expect(mockCommand.execute()).andReturn(true);
		replay(mockCommand);

		assertTrue(createLocalCommandInvoker().invoke(mockCommand));
	}

	@Override
	public void setBeanFactory(final BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

}